<style lang="scss" scoped>
  .form {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    .el-form-item {
      width: 250px;
    }
  }
</style>
<template>
  <el-dialog :title="title" :visible.sync="addDialogVisible" width="500px">
    <div class="ui-box leave_dialog">
      <el-form :model="repairItem" label-width="80px" :rules="formRule" ref="form" class="form">
        <el-form-item label="宿舍楼" prop="buildingNo">
          <el-select v-model="repairItem.buildingNo" size="mini" placeholder="请选择"
                     @change="handleChange"
                     clearable>
            <el-option
              v-for="item in buildingNoList"
              :key="item.buildingNo"
              :label="item.buildingNo"
              :value="item.buildingNo">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="宿舍号" prop="dorNo">
          <el-select v-model="repairItem.dorNo" size="mini" placeholder="请选择" clearable>
            <el-option
              v-for="item in dorNoList"
              :key="item.dorNo"
              :label="item.dorNo"
              :value="item.dorId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="报修种类" prop="repairKind">
          <el-select v-model="repairItem.repairKind" placeholder="请选择" size="mini">
            <el-option label="物资损坏" value="1"></el-option>
            <el-option label="网络故障" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="报修原因" prop="repairReason">
          <el-input type="textarea" size="mini" v-model="repairItem.repairReason"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false" size="mini">取 消</el-button>
        <el-button type="primary" @click="submit" size="mini">确 定</el-button>
      </div>
    </div>
  </el-dialog>
</template>
<script>
  export default {
    name: 'addItem',
    props: {//父组件传来的参数
      repairItem: {
        repairKind: '',
        repairReason: '',
        buildingNo: '',
        dorNo: ''
      },
    },
    data() {
      return {
        formRule: {
          buildingNo: [{required: true, message: '请输入宿舍楼号', trigger: 'blur'}],
          dorNo: [{required: true, message: '请输入宿舍号', trigger: 'blur'}],
          repairKind: [{required: true, message: '请输入报修种类', trigger: 'blur'}],
          repairReason: [{required: true, message: '请输入报修原因', trigger: 'blur'}],
        },
        title: '',
        addDialogVisible: false,
        buildingNoList: [],
        dorNoList: [],
        total: 0,
      }
    },
    created() {
      this.getBuildingNo()
    },
    methods: {
      submit() {
        this.$refs['form'].validate((val) => {
          console.log(val)
          if (val) {
            this.$emit('submit')
          }
        })
      },
      getBuildingNo() {
        this.tabLoading = true
        post('/pms_api/dormitory/list', {'type': 1}).then(res => {
          this.buildingNoList = res.data
          this.total = res.totalPages
          this.tabLoading = false;
        }).catch(err => {
          this.tabLoading = false
          console.log(err)
        })
      },
      handleChange(buildingNo) {
        this.tabLoading = true
        this.repairItem.dorNo = ''
        post('/pms_api/dormitory/list', {'buildingNo': buildingNo}).then(res => {
          this.dorNoList = res.data
          this.total = res.totalPages
          this.tabLoading = false;
        }).catch(err => {
          this.tabLoading = false
          console.log(err)
        })
      },
      openWindow(value) {
        this.title = value;
        this.addDialogVisible = true;
        this.dorNoList = null;
        this.repairItem.buildingNo = ''
        this.repairItem.dorNo = ''
        this.repairItem.repairKind = ''
        this.repairItem.repairReason = ''
      }
    }
  }
</script>

